package com.huike.clues.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huike.clues.domain.mpVO.MPTbActivityVO;
import com.huike.clues.domain.vo.MPTableDataInfoActivityListVo;
import com.huike.clues.domain.vo.TableData;
import com.huike.clues.mapper.ActivityMapper;
import com.huike.clues.service.MpActivityService;
import com.huike.common.core.page.PageDomain;
import com.huike.common.core.page.TableSupport;
import com.huike.common.entity.MPSysDictType;
import com.huike.common.entity.MPTbActivity;
import com.huike.common.utils.StringUtils;
import com.huike.common.utils.bean.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Author hxd
 * @Date 2023-08-29 9:18
 */
@Service
@Slf4j
public class MpActivityServiceImpl extends ServiceImpl<ActivityMapper, MPTbActivity> implements MpActivityService {
    @Resource
    private ActivityMapper activityMapper;

    /**
     * 新增活动管理
     * @param mpTbActivity
     */
    @Override
    public void add(MPTbActivity mpTbActivity) {
        log.info("新增活动管理:{}",mpTbActivity);
        int insert = activityMapper.insert(mpTbActivity);
        System.out.println(insert);
    }



    /**
     * 查询活动管理列表
     * @param tbActivity
     * @return
     */
    @Override
    public Page<MPTbActivity> actibityList(MPTbActivity tbActivity) {

        PageDomain pageDomain = TableSupport.buildPageRequest();
        PageHelper.startPage(pageDomain.getPageNum(),pageDomain.getPageSize());

        QueryWrapper<MPTbActivity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(tbActivity.getCode()),"code",tbActivity.getCode());
        wrapper.eq(StringUtils.isNotEmpty(tbActivity.getChannel()),"channel",tbActivity.getChannel());
        if(tbActivity.getParams()!= null){
            wrapper.ge(ObjectUtils.isNotEmpty(tbActivity.getParams().get("beginCreateTime")),"create_time",tbActivity.getParams().get("beginCreateTime"));
            wrapper.le(ObjectUtils.isNotEmpty(tbActivity.getParams().get("endCreateTime")),"create_time",tbActivity.getParams().get("endCreateTime"));
            wrapper.ge(ObjectUtils.isNotEmpty(tbActivity.getParams().get("beginTime")),"begin_time",tbActivity.getParams().get("beginTime"));
            wrapper.le(ObjectUtils.isNotEmpty(tbActivity.getParams().get("endTime")),"end_time",tbActivity.getParams().get("endTime"));

        }
        List<MPTbActivity> mpSysDictTypeList = activityMapper.selectList(wrapper);
        Page<MPTbActivity> pageList = (Page<MPTbActivity>) mpSysDictTypeList;

        return pageList;





















        //PageHelper.startPage(pageNum,pageSize);

//        List<MPTbActivity> list = activityMapper.selectList(null);
        //List<MPTbActivity> list = activityMapper.MyselectList(tbActivity);
       // Page<MPTbActivity> page = (Page) list;
//        Page<Object> p = new Page<>(pageNum, pageSize);
//        activityMapper.selectPage(p,null);

        //1. 设置当前页与页面大小
       // Page<MPTbActivity> page =new Page<>(pageNum,pageSize); //当前页1  页面大小是2

        //2. 创建分页需要条件
       // QueryWrapper wrapper = new QueryWrapper();

        //3. 调用selectPage方法实现分页，分页的结果会被封装到Page对象中
       // List<MPTbActivity> list = activityMapper.selectPage(page, wrapper);

//          List<MPTbActivity> list = activityMapper.selectList(null);

        //QueryWrapper<MPTbActivity> wrapper = new QueryWrapper<>();
       /* wrapper.eq("status","1");
        Integer toAudit = activityMapper.selectCount(wrapper);
        wrapper.eq("status","3");
        Integer reject = activityMapper.selectCount(wrapper);
        wrapper.eq("status","2");
        Integer pass = activityMapper.selectCount(wrapper);
        wrapper.eq("status","4");
        Integer finish = activityMapper.selectCount(wrapper);
        TableData tableData = new TableData();
        tableData.setToAudit(toAudit);
        tableData.setReject(reject);
        tableData.setPass(pass);
        tableData.setFinish(finish);

        return MPTableDataInfoActivityListVo.builder()
                .total(page.getTotal())
                .params(tableData)
                .rows(page.getResult())
                .build();*/
    }

    /**
     * 删除活动管理
     * @param ids
     */
    @Override
    public void delete(List<String> ids) {
        activityMapper.deleteBatchIds(ids);
    }

    /**
     * 修改活动管理
     * @param mpTbActivity
     */
    @Override
    public void update1(MPTbActivity mpTbActivity) {
        activityMapper.updateById(mpTbActivity);
    }

    /**
     * 获取活动管理详情信息
     * @param id
     * @return
     */
    @Override
    public MPTbActivity findId(Integer id) {
        MPTbActivity mpTbActivity = activityMapper.selectById(id);
        return mpTbActivity;
    }

    /**
     * 获取状态为2的渠道活动列表
     *
     * @param channel
     * @return
     */
    @Override
    public List<MPTbActivityVO> listselect(String channel) {
        QueryWrapper<MPTbActivity> wrapper = new QueryWrapper<>();
        wrapper.eq("channel",channel);
        List<MPTbActivity> list = activityMapper.selectList(wrapper);
        List<MPTbActivityVO> mpTbActivityVOList = new ArrayList<>();
        BeanUtils.copyBeanProp(list,mpTbActivityVOList);
        return mpTbActivityVOList;
    }
}
